<script>
import { mapState } from 'vuex'
import { paymentMethodArray } from '@/constant/cost'
import { updatePropertyfeeAPI } from '@/api/propety'

export default {
  name: 'feeForm',
  props: {
    edit: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      /* 表单数据对象 */
      formData: {
        enterpriseId: '',
        buildingId: '',
        startTime: '',
        endTime: '',
        paymentAmount: '',
        paymentMethod: ''
      },
      /* 表单规则对象 */
      formRules: {
        enterpriseId: [
          { required: true, message: '请选择租户', trigger: 'change' }
        ],
        buildingId: [
          { required: true, message: '请选择租赁楼宇', trigger: 'change' }
        ],
        paymentAmount: [
          { required: true, message: '请输入支付金额', trigger: 'blur' }

        ],
        paymentMethod: [
          { required: true, message: '请选择支付方式', trigger: 'change' }
        ]
      },
      time: [],
      paymentMethodArray
    }
  },
  methods: {
    /* 获取租赁楼宇列表的回调函数 */
    async fetchBuildingList() {
      try {
        await this.$store.dispatch('cost/fetchBuildingList')
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /* 获取企业列表的回调函数 */
    async fetchEnterpriseList() {
      try {
        await this.$store.dispatch('cost/fetchEnterpriseList')
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    findFee(row) {
      this.formData = { ...row }
      this.time = [row.startTime, row.endTime]
    },
    /* 点击确定按钮的回调函数 */
    async handleSubmit() {
      const valid = await this.$refs.formRef.validate().catch(error => error)
      if (!valid) return
      this.formData.startTime = this.time[0]
      this.formData.endTime = this.time[1]
      if (this.edit) {
        try {
          const { msg } = await updatePropertyfeeAPI(this.formData)
          this.$emit('success')
          this.$message.success(msg)
        } catch (error) {
          this.$message.error(error.message)
        }
      } else {
        try {
          const msg = await this.$store.dispatch('cost/addPropertyfee', this.formData)
          this.$emit('success')
          this.$message.success(msg)
        } catch (error) {
          this.$message.error(error.message)
        }
      }
    },

  },
  computed: {
    ...mapState('cost', ['buildingList', 'enterpriseList'])
  },
  emits: ['success'],
  created() {
    this.fetchBuildingList()
    this.fetchEnterpriseList()
  }
}
</script>

<template>
  <el-form label-width="100px" :model="formData" :rules="formRules" ref="formRef">
    <el-form-item label="选择租户" prop="enterpriseId">
      <el-select size="small" v-model="formData.enterpriseId" placeholder="请选择租户">
        <el-option v-for="(item) in enterpriseList" :value="item.id" :label="item.name" :key="item.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="租赁楼宇" prop="buildingId">
      <el-select size="small" v-model="formData.buildingId" placeholder="请选择租赁楼宇">
        <el-option v-for="(item) in buildingList" :value="item.id" :label="item.name" :key="item.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="缴费日期">
      <el-date-picker size="small" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" v-model="time" ></el-date-picker>
    </el-form-item>
    <el-form-item label="支付金额" prop="paymentAmount">
      <el-input size="small" placeholder="请输入支付金额" v-model="formData.paymentAmount"></el-input>
    </el-form-item>
    <el-form-item label="支付方式" prop="paymentMethod">
      <el-select size="small" v-model="formData.paymentMethod" placeholder="请选择支付方式">
        <el-option v-for="(item) in paymentMethodArray" :value="item.value" :label="item.label" :key="item.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button size="small">取 消</el-button>
      <el-button size="small" type="primary" @click="handleSubmit">确 定</el-button>
    </el-form-item>
  </el-form>
</template>

<style scoped lang="scss">
.el-form {
  width: 70%;

  .el-date-editor {
    width: 100%;
  }

  .el-select {
    width: 100%;

  }
}
</style>
